        // SPI_PT1000_Init();
        // uint8_t writeData[1] = {100};
        // PT1000WriteRegisters(0x86, writeData, 1);
        // // DelayMs(1);
        // uint8_t buffers[1] = {0};
        // PT1000ReadCommand(0x06, buffers, 1);
        /*
        RTD_Init();
        RTD_Write_Command(0x86, 0x64);
        RTD_Read_Data(0x06);
        */


/*
#define RTD_CS_Clr() hal_gpio_write(PT1000_NSS_PIN, 0)
#define RTD_CS_Set() hal_gpio_write(PT1000_NSS_PIN, 1)
#define RTD_CLK_Clr() hal_gpio_write(PT1000_CLK_PIN, 0)
#define RTD_CLK_Set() hal_gpio_write(PT1000_CLK_PIN, 1)
#define RTD_SDI_Clr() hal_gpio_write(PT1000_MISO_PIN, 0)
#define RTD_SDI_Set() hal_gpio_write(PT1000_MISO_PIN, 1)

void RTD_Init(void)
{
    hal_gpio_pin_init(PT1000_CLK_PIN, GPIO_OUTPUT);
    hal_gpio_pin_init(PT1000_NSS_PIN, GPIO_OUTPUT);
    hal_gpio_pin_init(PT1000_MOSI_PIN, GPIO_OUTPUT);
    hal_gpio_pin_init(PT1000_MISO_PIN, GPIO_INPUT);
}

void RTD_Write_Command(uint8_t adr, uint8_t cmd)
{
    hal_gpio_write(PT1000_NSS_PIN, 0); // 将 CS 置为低电平

    // 逐字节发送地址和命令
    for (int i = 7; i >= 0; i--)
    {
        RTD_CLK_Set(); // 拉高时钟线
        if (adr & (1 << i))
        {
            RTD_SDI_Set(); // 设置数据线
        }
        else
        {
            RTD_SDI_Clr(); // 清空数据线
        }
        RTD_CLK_Clr(); // 拉低时钟线
    }

    for (int i = 7; i >= 0; i--)
    {
        RTD_CLK_Set(); // 拉高时钟线
        if (cmd & (1 << i))
        {
            RTD_SDI_Set(); // 设置数据线
        }
        else
        {
            RTD_SDI_Clr(); // 清空数据线
        }
        RTD_CLK_Clr(); // 拉低时钟线
    }

    hal_gpio_write(PT1000_NSS_PIN, 1); // 将 CS 置为高电平
}

uint8_t RTD_Read_Data(uint8_t adr)
{
    uint8_t data = 0;

    hal_gpio_write(PT1000_NSS_PIN, 0); // 将 CS 置为低电平

    for (int i = 7; i >= 0; i--)
    {
        RTD_CLK_Set(); // 拉高时钟线
        if (adr & (1 << i))
        {
            RTD_SDI_Set(); // 设置数据线
        }
        else
        {
            RTD_SDI_Clr(); // 清空数据线
        }
        RTD_CLK_Clr(); // 拉低时钟线
    }

    for (int i = 7; i >= 0; i--)
    {
        RTD_CLK_Set(); // 拉高时钟线
        // 从MISO读取数据
        data |= (hal_gpio_read(PT1000_MISO_PIN) << i);
        RTD_CLK_Clr(); // 拉低时钟线
    }

    hal_gpio_write(PT1000_NSS_PIN, 1); // 将 CS 置为高电平

    return data;
}
*/


void PT1000IoInit(void)
{
    hal_gpio_pin_init(PT1000_NSS_PIN, GPIO_OUTPUT);
    hal_gpioretention_register(PT1000_NSS_PIN);
}

uint8_t SPI_PT1000_Init(void)
{
    PT1000IoInit();
    uint8_t retval = PPlus_SUCCESS;

    retval = hal_spi_bus_init(&spiPT1000_spi, spi_pt1000_cfg);

    // LOG("%s:%s==%d==%d\r\n",__FILE__,__FUNCTION__,__LINE__,retval);
    if (retval != PPlus_SUCCESS)
    {
        LOG("spi init err!please check it! retval = %d\n", retval);
        hal_pwrmgr_get_module_lock_status();

        // g_rxtimeout_count = MCU_RESET_CYCLE;

        return retval;
    }

    return retval;
}


uint8_t PT1000ReadCommand(uint8_t command, uint8_t *buffer, uint16_t size)
{
    uint8_t status = 0;
    uint8_t tx_buf[1] = {0};
    uint8_t rx_buf[2] = {0};

    hal_gpio_write(PT1000_NSS_PIN, 0);

    tx_buf[0] = (uint8_t)command;
    hal_spi_send_byte(&spiPT1000_spi, tx_buf[0]);
    hal_spis_read_rxn(&spiPT1000_spi, rx_buf, 1);

    for (uint16_t i = 0; i < size; i++)
    {
        tx_buf[0] = 0xFF;
        hal_spi_send_byte(&spiPT1000_spi, tx_buf[0]);
        hal_spis_read_rxn(&spiPT1000_spi, &buffer[i], 1);
    }

    hal_gpio_write(PT1000_NSS_PIN, 1);

    // log the buffer data
    for (uint16_t i = 0; i < size; i++)
    {
        LOG("[PT1000ReadCommand] data[%d] = %X\n", i, buffer[i]);
    }

    return status;
}

void PT1000WriteRegisters(uint16_t command, uint8_t *buffer, uint16_t size)
{
    uint8_t tx_buf[1] = {0};
    uint8_t rx_buf[2] = {0};

    hal_gpio_write(PT1000_NSS_PIN, 0);

    tx_buf[0] = command;
    hal_spi_send_byte(&spiPT1000_spi, tx_buf[0]);
    hal_spis_read_rxn(&spiPT1000_spi, rx_buf, 1);

    hal_spi_send_byte(&spiPT1000_spi, buffer[0]);
    hal_spis_read_rxn(&spiPT1000_spi, rx_buf, 1);

    hal_gpio_write(PT1000_NSS_PIN, 1);

    LOG("[PT1000WriteRegisters] rx_buf[%X, %X], data[%X]\n", rx_buf[0], rx_buf[1], buffer[0]);
}


// max31865
// 初始化 SPI 总线
void max31865_init_spi(void)
{
    spi_Cfg_t spi_config;
    // 配置 SPI 参数
    spi_config.sclk_pin = PT1000_CLK_PIN;
    spi_config.ssn_pin = PT1000_NSS_PIN;
    spi_config.MOSI = PT1000_MOSI_PIN;
    spi_config.MISO = PT1000_MISO_PIN;
    spi_config.baudrate = 1000000;    // 1MHz
    spi_config.spi_scmod = SPI_MODE1; // 根据 MAX31865 的时钟极性和相位设置
    spi_config.spi_tmod = SPI_TRXD;   // 传输模式为同时发送和接收

    // 初始化 SPI
    hal_spi_init((&spiPT1000_spi)->spi_index);
    hal_spi_bus_init(&spiPT1000_spi, spi_config);
}

// 读取指定寄存器的数据
uint8_t max31865_read_register(uint8_t reg_address)
{
    // 构建读取数据的请求
    uint8_t tx_data[2] = {reg_address, 0x00};
    uint8_t rx_data[2] = {0x00, 0x00};

    // 进行 SPI 读取操作
    hal_spi_transmit(&spiPT1000_spi, SPI_TRXD, tx_data, rx_data, 2, 2);

    // 打印读取到的寄存器数据
    LOG("read reg[%X] = %X\n", reg_address, rx_data[1]);

    // 返回读取到的数据
    return rx_data[1];
}

// 写入指定寄存器的数据
void max31865_write_register(uint8_t reg_address, uint8_t data)
{
    // 构建写入数据的请求
    uint8_t tx_data[2] = {reg_address | 0x80, data};

    // 进行 SPI 写入操作
    hal_spi_transmit(&spiPT1000_spi, SPI_TXD, tx_data, NULL, 2, 0);
}